﻿//////////////////////////////////////////////////////////////////////////
// DirectDrawer.h
//////////////////////////////////////////////////////////////////////////
#ifndef __DDRAW_DEC_H__
#define __DDRAW_DEC_H__

#include "GeneralTypes.h"
#include "colorspace.h"
// please remember!!! to debug this application you have to install directxsdk // http://www.google.com/search?hl=en&q=directx+sdk
#include <wingdi.h>
#include <ddraw.h>

#if defined(_WIN64)
#pragma comment(lib, "./lib64/ddraw64.lib")
#pragma comment(lib, "./lib64/dxguid64.lib")
#else
#pragma comment(lib, "./library/dxguid.lib")
#pragma comment(lib, "./library/ddraw.lib")
#endif

#define ICON_WIDTH_SIZE		30
#define ICON_HEIGHT_SIZE	30
#define ICON_H_GAP			5
#define ZOOM_FUNCTION_VER	2
//==============================================================================
class CDirectDrawer
{
public:
	enum { DD_BORDER = 2 };
	CDirectDrawer();
	~CDirectDrawer();
	BOOL	InitDirectDraw( HWND hWnd, bool bTryOverlay );
	void	CloseDirectDraw();
	void	BlitBitmap( const PRESENTATION_ITEM* pcPresentItem, BOOL bDraw = TRUE );
	void	DDrawOnPaint();
	void	DDrawOnSize(UINT state, int cx, int cy);
	void	DDrawOnMove(int x, int y);
	void	 GetDDrawSizes(unsigned int & h, unsigned int & v);
	COLORREF GetBkGroundColor();
	void	 GetDisplayMode( int& iDisplayCx, int& iDisplayCy, int& iFrameRate );
	HWND	GetHWnd() { return hDDrawWindow;}
	HDC		GetDrawDC();
	void	ReleaseDC(HDC hDC );
	HDC		GetOffScrDC();
	void	ReleaseOffScrDC( HDC hDC );
	void	CloseSurface();
private:
	BOOL	CreateSurfaces(bool);
	BOOL	CreateDCSurface(bool);
	void	ShowMessage(int n, char * message, char * title);
	DWORD	ConvertGDIColor(COLORREF dwGDIColor, LPDIRECTDRAWSURFACE7 pdds);
	bool	GetDestRect(RECT * prcDst);
	bool	GetWndClientRect(RECT * prcWnd);
	bool	GetWndTargetRect(RECT & rcWnd, RECT * prcTarget);
	bool	GetWndRect(RECT * prcWnd);
	bool	CreateOverlaySurface(unsigned int width, unsigned int height, int type);
	bool	CreateOffscreenSurface(unsigned int width, unsigned int height, int type );
	bool	CreateOffscreenSurfaceEx(LPDIRECTDRAWSURFACE7 * pDDS, DWORD dwFlags=0, DWORD dwCaps=DDSCAPS_VIDEOMEMORY | DDSCAPS_LOCALVIDMEM, unsigned int width=0, unsigned int height=0, int type =0 );
	void	AdjustSizeForHardwareLimits(RECT * prcSrc, RECT * prcDst);
	bool	CreateZoomSurface( int width, int height, int type );
	void	CloseZoomSurface();
	bool	GetZoomDestRect( RECT * prcDst );
	bool	GetZoomAreaRect( RECT * prcDst );
	bool	GetZoomSrcRect( RECT * prcSrc );

private:
	static const COLORREF crDDrawBackgroundColor;
	LPDIRECTDRAW7        g_pDD;			//
	LPDIRECTDRAWSURFACE7 g_pDDSPrimary;	// 
	LPDIRECTDRAWSURFACE7 g_pDDSOffscr;	//
	LPDIRECTDRAWSURFACE7 g_pDDSBack;	//
	LPDIRECTDRAWSURFACE7 g_pDDSDCBuffer;
	LPDIRECTDRAWSURFACE7 g_pZoomSurface;	//for zoom

	DDOVERLAYFX         g_OverlayFX;
	DWORD               g_dwOverlayFlags;
	BOOL				m_UseVideoMemory;
	BOOL				m_UsePixelFormat;
	DWORD				m_dwNumCodes;
	DWORD		*		m_lpCodes;
	bool				m_needChangeSize;
	int					h_size_wnd;
	int					v_size_wnd;
	int					m_colZoomRate;
private:
	DDSURFACEDESC2		g_ddsd;
	BOOL				m_bNOAspact;
	BOOL				m_bEnableZoom;
	BOOL				m_bShowZoomWnd;
	BOOL				bSurfaceEnumerated;
	RECT				m_rcZoomWnd;
	RECT				m_rcZoomArea;
	int					m_iZoomRate;

public:
	bool				fUseOverlay;
	bool				fEXCLUSIVE;

	DDCAPS				drivercaps;
	DDCAPS				helcaps;

	unsigned int		h_size_wnd_ddraw;
	unsigned int		v_size_wnd_ddraw;

	int					overlay_surface_type;
	HWND				hDDrawWindow;

	ColorSpaceDll& m_csdll;
	BOOL		GetCopyData( BYTE * dataBuff, DWORD buffLen );
	BYTE *		GetScreenData();
	DWORD		getRGBBitCount() { return g_ddsd.ddpfPixelFormat.dwRGBBitCount; }
	DWORD		getStrid() { return g_ddsd.lPitch; }
	void		SetFitArea( BOOL bNoAspact = TRUE ) { m_bNOAspact = bNoAspact; }
	BOOL		IsFitSize() { return m_bNOAspact; }
	void		SetEnableZoom( BOOL bEnable, BOOL bVisiable ) { m_bEnableZoom = bEnable; m_bShowZoomWnd = bVisiable; }
	void		SetZoomWindow( RECT & rcWnd ) { m_rcZoomWnd = rcWnd; }
	BOOL		IsZoomEnable() { return m_bEnableZoom; }
	void		SetZoomArea( RECT & rcZoom ) { m_rcZoomArea = rcZoom; }
	void		SetiZoomRate( int i_rate ) { m_iZoomRate = i_rate; }
	void		SetZoomRateColor( int osdColor ) { m_colZoomRate = osdColor; }
	void		DrawZoomRate( HDC hDC );

};
#endif // __DDRAW_DEC_H__
